{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph,Node,Relationship,NodeMatcher\n",
    "\n",
    "class Helper():\n",
    "    def __init__(self):\n",
    "        uri = \"neo4j+s://df2f7571.databases.neo4j.io:7687\"\n",
    "        user = \"neo4j\"\n",
    "        password = \"kCApGshOca7wAddUzAf89OgCgAtU_A70TltIosZG_5Y\"\n",
    "       \n",
    "        self.graph = Graph(uri,auth=(user,password))\n",
    "        print(self.graph)\n",
    "\n",
    "        ##执行一次查询，确定是否成功连接数据库\n",
    "        result = self.graph.run(\"MATCH (p:Person) RETURN p.name\")\n",
    "        for record in result:\n",
    "            if record[\"p.name\"] ==\"Alice\":\n",
    "                print(\"数据库连接成功！\\n\")\n",
    "            else:\n",
    "                print(\"数据库连接失败！\\n\")\n",
    "\n",
    "    def execute_cypher(self, cypher_query):\n",
    "        result = self.graph.run(cypher_query)\n",
    "        return result\n",
    "    \n",
    "    def selectByNodeName(self,node_name,selectType):\n",
    "        matcher = NodeMatcher(self.graph)\n",
    "\n",
    "        node = matcher.match(selectType,name=node_name).first()\n",
    "\n",
    "        return node\n",
    "    \n",
    "            "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Graph('neo4j+s://df2f7571.databases.neo4j.io:7687')\n"
     ]
    },
    {
     "ename": "ServiceUnavailable",
     "evalue": "Cannot connect to any known routers",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mServiceUnavailable\u001b[0m                        Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_11536\\456703232.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mh\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mHelper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m      2\u001b[0m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mh\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mselectByNodeName\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Alice\"\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;34m\"Person\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m      3\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_11536\\2667629903.py\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m     11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     12\u001b[0m         \u001b[1;31m##执行一次查询，确定是否成功连接数据库\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 13\u001b[1;33m         \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgraph\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"MATCH (p:Person) RETURN p.name\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     14\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mrecord\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mresult\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     15\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mrecord\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m\"p.name\"\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m==\u001b[0m\u001b[1;34m\"Alice\"\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\database.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, cypher, parameters, **kwparameters)\u001b[0m\n\u001b[0;32m    403\u001b[0m         \u001b[1;33m:\u001b[0m\u001b[1;32mreturn\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    404\u001b[0m         \"\"\"\n\u001b[1;32m--> 405\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mauto\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrun\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcypher\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwparameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    406\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    407\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mevaluate\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcypher\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparameters\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwparameters\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\database.py\u001b[0m in \u001b[0;36mrun\u001b[1;34m(self, cypher, parameters, **kwparameters)\u001b[0m\n\u001b[0;32m    989\u001b[0m                 result = self._connector.auto_run(cypher, parameters,\n\u001b[0;32m    990\u001b[0m                                                   \u001b[0mgraph_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgraph\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 991\u001b[1;33m                                                   readonly=self.readonly)\n\u001b[0m\u001b[0;32m    992\u001b[0m             \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_connector\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpull\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    993\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mCursor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mhydrant\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36mauto_run\u001b[1;34m(self, cypher, parameters, graph_name, readonly)\u001b[0m\n\u001b[0;32m   1336\u001b[0m             \u001b[0mcannot\u001b[0m \u001b[0mbe\u001b[0m \u001b[0mhonoured\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1337\u001b[0m         \"\"\"\n\u001b[1;32m-> 1338\u001b[1;33m         \u001b[0mcx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_acquire\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1339\u001b[0m         \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1340\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mcx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mauto_run\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcypher\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mparameters\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgraph_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreadonly\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mreadonly\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m_acquire\u001b[1;34m(self, graph_name, readonly)\u001b[0m\n\u001b[0;32m   1109\u001b[0m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_acquire_ro\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1110\u001b[0m         \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1111\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_acquire_rw\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1112\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1113\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0m_acquire_ro\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgraph_name\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m_acquire_rw\u001b[1;34m(self, graph_name)\u001b[0m\n\u001b[0;32m   1201\u001b[0m         \u001b[1;32mwhile\u001b[0m \u001b[1;32mTrue\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1202\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1203\u001b[1;33m             \u001b[0mro_profiles\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrw_profiles\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_get_profiles\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mreadonly\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1204\u001b[0m             \u001b[1;32mif\u001b[0m \u001b[0mrw_profiles\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1205\u001b[0m                 \u001b[1;31m# There is at least one writer, so collect the pools\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36m_get_profiles\u001b[1;34m(self, graph_name, readonly)\u001b[0m\n\u001b[0;32m   1014\u001b[0m                     \u001b[0mrt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwait_until_updated\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1015\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1016\u001b[1;33m                 \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrefresh_routing_table\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgraph_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1017\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1018\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mrefresh_routing_table\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgraph_name\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32md:\\Anaconda3\\envs\\learn\\lib\\site-packages\\py2neo\\client\\__init__.py\u001b[0m in \u001b[0;36mrefresh_routing_table\u001b[1;34m(self, graph_name)\u001b[0m\n\u001b[0;32m   1062\u001b[0m                         \u001b[0mcx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mrelease\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1063\u001b[0m             \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1064\u001b[1;33m                 \u001b[1;32mraise\u001b[0m \u001b[0mServiceUnavailable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Cannot connect to any known routers\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1065\u001b[0m         \u001b[1;32mfinally\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1066\u001b[0m             \u001b[0mrt\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mset_not_updating\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mServiceUnavailable\u001b[0m: Cannot connect to any known routers"
     ]
    }
   ],
   "source": [
    "h = Helper()\n",
    "result = h.selectByNodeName(\"Alice\",\"Person\")\n",
    "result"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "str(result.labels).strip(':')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph,Node,Relationship,NodeMatcher,RelationshipMatcher\n",
    "\n",
    "def connect():\n",
    "    \"\"\"\n",
    "    初始化连接数据库\n",
    "    \"\"\"\n",
    "    uri = \"neo4j+s://df2f7571.databases.neo4j.io\"\n",
    "    user = \"neo4j\"\n",
    "    password = \"kCApGshOca7wAddUzAf89OgCgAtU_A70TltIosZG_5Y\"\n",
    "    \n",
    "    graph = Graph(uri,auth=(user,password))\n",
    "\n",
    "    ##执行一次查询，确定是否成功连接数据库\n",
    "    result = graph.run(\"MATCH (p:Person) RETURN p.name\")\n",
    "    for record in result:\n",
    "        if record[\"p.name\"] ==\"Alice\":\n",
    "            print(\"数据库连接成功！\\n\")\n",
    "        else:\n",
    "            print(\"数据库连接失败！\\n\")\n",
    "    return graph\n",
    "def getPropertyOnNode(node):\n",
    "    \"\"\"\n",
    "    以字典形式返回结点的所有属性\n",
    "    \"\"\"\n",
    "    print(node)\n",
    "    p_dict = dict(node.items())\n",
    "    return p_dict\n",
    "\n",
    "def selectNodeByID(nodeID,selectType):\n",
    "    \"\"\"\n",
    "    根据结点ID返回数据库匹配的唯一结点\n",
    "    \"\"\"\n",
    "    graph = connect()\n",
    "    matcher = NodeMatcher(graph)\n",
    "    if selectType != \"疾病\":\n",
    "        node = matcher.match(selectType,ID=nodeID).first()\n",
    "    else:\n",
    "        node = matcher.match(selectType,id=nodeID).first()\n",
    "\n",
    "    return node\n",
    "\n",
    "def getNodeRelation(nodeID,selectType):\n",
    "    \"\"\"\n",
    "    根据结点和结点类型获取需要的关系，不同的结点类型用不同的关系\n",
    "    \"\"\"\n",
    "    relationDict = {'中药组方':[\"方剂组成\"],\"中药材\":[\"药材组分\",\"药材来源植物名\"],\"化合物\":[\"化合物对应靶点\"],\"靶点\":[\"化合物对应靶点\",\"靶点对应疾病\"],\"疾病\":[\"靶点对应疾病\"]}\n",
    "    try:\n",
    "        node = selectNodeByID(nodeID,selectType)\n",
    "    except:\n",
    "        print(\"结点未找到！\")\n",
    "    relations = relationDict.get(selectType,[])\n",
    "\n",
    "    if selectType == '疾病':\n",
    "        direction = 1\n",
    "    else:\n",
    "        direction = 0 \n",
    "    return node, relations,direction\n",
    "\n",
    "def selectNodeByRelation(node,relation,direction=0):\n",
    "    \"\"\"\n",
    "    根据关系查找和结点node相连的所有结点\n",
    "    \"\"\"\n",
    "    print(node,relation)\n",
    "    graph = connect()\n",
    "    rel_matcher = RelationshipMatcher(graph)\n",
    "    print('dire',direction)\n",
    "    # 查找节点A的所有R关系\n",
    "    if direction == 0:\n",
    "        rels = rel_matcher.match((node,), r_type=relation)\n",
    "    else:\n",
    "        rels = rel_matcher.match((None,node), r_type=relation)\n",
    "    print(rels.count())\n",
    "\n",
    "    # 遍历所有R关系，获取关系的另一个节点\n",
    "    nodes = []\n",
    "    for rel in rels:\n",
    "        if rel.start_node == node:\n",
    "            nodes.append(rel.end_node)  \n",
    "        elif rel.end_node == node:\n",
    "            nodes.append(rel.start_node)\n",
    "    return nodes\n",
    "\n",
    "def getRelationNodeList(nodes):\n",
    "    print(nodes)\n",
    "    result = []\n",
    "    for node in nodes:\n",
    "        temp = {}\n",
    "        if '中文名' in node.keys():\n",
    "            temp['name'] = getPropertyOnNode(node)['中文名']\n",
    "        else:\n",
    "            temp['name'] = getPropertyOnNode(node)['name']\n",
    "        if 'ID' in node.keys():\n",
    "            temp['id'] = getPropertyOnNode(node)['ID']\n",
    "        else: \n",
    "            temp['id'] = getPropertyOnNode(node)['id']\n",
    "        result.append(temp)\n",
    "    return result\n",
    "\n",
    "def relationNodeTransfer(nodeID,selectType):\n",
    "    result = []\n",
    "    node,relations, direction = getNodeRelation(nodeID,selectType)\n",
    "    if relations:\n",
    "        if len(relations)>1:\n",
    "            for rel in relations:\n",
    "                if rel == '化合物对应靶点':\n",
    "                    direction = 1\n",
    "                else:\n",
    "                    direction = 0\n",
    "                nodes = selectNodeByRelation(node,rel,direction)\n",
    "                result.append(getRelationNodeList(nodes))\n",
    "        else:\n",
    "            nodes = selectNodeByRelation(node,relations[0],direction)\n",
    "            result.append(getRelationNodeList(nodes))\n",
    "        return result\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = relationNodeTransfer('target0011160','靶点')\n",
    "a"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## all\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from py2neo import Graph,Node,Relationship,NodeMatcher,RelationshipMatcher\n",
    "\n",
    "class Helper():\n",
    "    def __init__(self):\n",
    "        \"\"\"\n",
    "        初始化连接数据库\n",
    "        \"\"\"\n",
    "        uri = \"neo4j+s://df2f7571.databases.neo4j.io\"\n",
    "        user = \"neo4j\"\n",
    "        password = \"kCApGshOca7wAddUzAf89OgCgAtU_A70TltIosZG_5Y\"\n",
    "       \n",
    "        self.graph = Graph(uri,auth=(user,password))\n",
    "\n",
    "        ##执行一次查询，确定是否成功连接数据库\n",
    "        result = self.graph.run(\"MATCH (p:Person) RETURN p.name\")\n",
    "        for record in result:\n",
    "            if record[\"p.name\"] ==\"Alice\":\n",
    "                print(\"数据库连接成功！\\n\")\n",
    "            else:\n",
    "                print(\"数据库连接失败！\\n\")\n",
    "\n",
    "    def execute_cypher(self, cypher_query):\n",
    "        \"\"\"\n",
    "        执行cypher语句\n",
    "        \"\"\"\n",
    "        result = self.graph.run(cypher_query)\n",
    "        return result\n",
    "    \n",
    "    def selectNodeByNodeName(self,node_name,selectType):\n",
    "        \"\"\"\n",
    "        根据标签和结点名称获取查询到的第一个结点\n",
    "        \"\"\"\n",
    "        matcher = NodeMatcher(self.graph)\n",
    "        node = matcher.match(selectType,name=node_name).first()\n",
    "        return node\n",
    "    \n",
    "    def selectNodeByRelation(self,node,relation):\n",
    "        \"\"\"\n",
    "        根据关系查找和结点node相连的所有结点\n",
    "        \"\"\"\n",
    "        rel_matcher = RelationshipMatcher(self.graph)\n",
    "\n",
    "        # 查找节点A的所有R关系\n",
    "        rels = rel_matcher.match((node,), r_type=relation)\n",
    "\n",
    "        # 遍历所有R关系，获取关系的另一个节点\n",
    "        nodes = []\n",
    "        for rel in rels:\n",
    "            if rel.start_node == node:\n",
    "                nodes.append(rel.end_node)\n",
    "            else:\n",
    "                nodes.append(rel.start_node)\n",
    "        return nodes\n",
    "\n",
    "    def getPropertyOnNode(self,node):\n",
    "        \"\"\"\n",
    "        以字典形式返回结点的所有属性\n",
    "        \"\"\"\n",
    "        p_dict = dict(node.items())\n",
    "        \n",
    "        return p_dict\n",
    "    "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "learn",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
